package com.hera.base.web.vaadin.mantenimiento;

import static com.hera.base.util.InputData.getStringData;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.hera.base.util.FiltroDTO;
import com.hera.base.util.TableCustom;
import com.hera.base.web.BaseUI;
import com.hera.base.web.MantenimientoMaestroDetalle;
import com.hera.process.config.domain.ParameterDetail;
import com.hera.process.config.rest.delegate.SecurityDelegate;
import com.hera.process.security.domain.SecGroupModule;
import com.hera.process.security.domain.SecGroupModulePK;
import com.hera.process.security.domain.SecurityGroup;
import com.vaadin.annotations.AutoGenerated;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.event.FieldEvents.TextChangeEvent;
import com.vaadin.event.FieldEvents.TextChangeListener;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.ListSelect;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Notification.Type;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

public class SecurityGroupUI extends BaseUI implements MantenimientoMaestroDetalle,ClickListener{

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	@AutoGenerated
	private VerticalLayout mainLayout;
	@AutoGenerated
	private VerticalLayout verticalLayout_2;
	@AutoGenerated
	private HorizontalLayout lytDetalle;
	@AutoGenerated
	private ListSelect listsModulos_Nuevos;
	@AutoGenerated
	private VerticalLayout verticalLayout_4;
	@AutoGenerated
	private Button btnAadir;
	@AutoGenerated
	private Button btnQuitar;
	@AutoGenerated
	private ListSelect ListsModulos;
	@AutoGenerated
	private HorizontalLayout lytMaestro;
	@AutoGenerated
	private VerticalLayout pnlFormulario;
	@AutoGenerated
	private HorizontalLayout lytBotonera;
	@AutoGenerated
	private Button btnEliminarMaestro;
	@AutoGenerated
	private Button btnGuardarMaestro;
	@AutoGenerated
	private TextField txtDescripcion;
	@AutoGenerated
	private TextField txtNombreMaestro;
	@AutoGenerated
	private Label label_1;
	@AutoGenerated
	private HorizontalLayout lytTablaMaestra;
	@AutoGenerated
	private TableCustom tblfMaestro;
	private static final Logger log = Logger.getLogger(SecurityGroupUI.class);
	
	private static final long serialVersionUID = 6354364049021506958L;
	
	@Autowired
	SecurityDelegate securityDelegate;
	boolean esNuevo;
	boolean esNuevoDetalle;
	SecurityGroup securitygroup;
	ParameterDetail listdetail;
	
	/**
	 * The constructor should first build the main layout, set the
	 * composition root and then do any custom initialization.
	 *
	 * The constructor will not be automatically regenerated by the
	 * visual editor.
	 */
	public SecurityGroupUI() {
		buildMainLayout();
		setCompositionRoot(mainLayout);
	}

	@Override
	protected void postConstruct() {

		cargarFormulario();
		buscarMaestro();
		buscarDetalle();
	}
	
	@Override
	public void cargarFormulario() {
		
		txtNombreMaestro.setNullRepresentation("");
		txtDescripcion.setNullRepresentation("");
		
		log.info("Inicio CargarFormulario");
		esNuevo=true;
		securitygroup=null;
		txtNombreMaestro.addStyleName("fieldRequired");
		
		btnEliminarMaestro.setVisible(false);
		btnEliminarMaestro.addClickListener(this);
		btnGuardarMaestro.addClickListener(this);
		btnGuardarMaestro.setCaption("Nuevo");
		
		btnQuitar.setVisible(true);
		btnQuitar.addClickListener(this);
		btnAadir.addClickListener(this);
		btnAadir.setVisible(true);
		
		tblfMaestro.setTitulo("Maestro de Security Group");
		tblfMaestro.setHeight("200px");
		
		tblfMaestro.addColumn("name", "Nombre", 100, true, String.class);
		tblfMaestro.addColumn("description", "Descripcion", 150, true, String.class);
		tblfMaestro.construir();
		
		tblfMaestro.addTextChangeListener(new TextChangeListener() {
			private static final long serialVersionUID = 1L;
			@Override
			public void textChange(TextChangeEvent event) {
				cargarFormulario();
				buscarMaestro(event);
			}
		});
		
		tblfMaestro.addValueChangeListener(new ValueChangeListener() {
			private static final long serialVersionUID = 1L;
			@Override
			public void valueChange(ValueChangeEvent event) {
						
				securitygroup=(SecurityGroup)tblfMaestro.getSelectedItem(event);
				esNuevo=(securitygroup==null);
				if(esNuevo){
					limpiarMaestro();
				}else{
					btnEliminarMaestro.setVisible(true);
					btnGuardarMaestro.setCaption("Modificar");
					txtNombreMaestro.setValue(securitygroup.getName());
					txtDescripcion.setValue(securitygroup.getDescription());
					buscarDetalle();
				}
				
			}
		});
		
	// LISTA DE MODULOS	
		ListsModulos.setHeight("100px");
		ListsModulos.setMultiSelect(true);
		listsModulos_Nuevos.setHeight("100px");
		listsModulos_Nuevos.setRows(15);
		listsModulos_Nuevos.setMultiSelect(true);
		listsModulos_Nuevos.setImmediate(true);
		
		
		ListsModulos.addValueChangeListener(new ValueChangeListener() {
			private static final long serialVersionUID = 1L;
			@Override
			public void valueChange(ValueChangeEvent event) {
				final String valueString = String.valueOf(event.getProperty()
                        .getValue());
                Notification.show("Value changed:", valueString,
                        Type.TRAY_NOTIFICATION);
				
			}
		});

	 
		
		
		
		
	}
	
	@Override
	public void buscarMaestro() {
		buscarMaestro(null);
	}

	@Override
	public void buscarMaestro(TextChangeEvent event) {
		log.info("inicio BuscarMaestro");
		FiltroDTO filtroDTO= tblfMaestro.getFiltroDTO(event);
		filtroDTO.addOrdenamientoAscendente("name");
		List<SecurityGroup> secgroup= securityDelegate.findAllSecurityGroups(filtroDTO);
		tblfMaestro.setLstData(secgroup);
		limpiarMaestro();
		log.info("fin buscarMaestro");
	}

	@Override
	public void limpiarMaestro() {
		log.info("Inicio LimpiarMaestro");
		btnEliminarMaestro.setVisible(false);
		btnGuardarMaestro.setCaption("Nuevo");
		txtNombreMaestro.setValue("");
		txtDescripcion.setValue("");
	}

	@Override
	public void eliminarMaestro() {
		log.info("Iniio EliminarMaestro");
		securityDelegate.deleteSecurityGroup(securitygroup);
		buscarMaestro();
		limpiarMaestro();
		log.info("fin EliminarMaestro");
	}

	@Override
	public void guardarMaestro() {
		log.info("Inicio GuardarMaestro");
		if(esNuevo){
			securitygroup=new SecurityGroup();
			obtenerDatosFormulario(securitygroup);
			securityDelegate.createSecurityGroup(securitygroup);
		}else {
			obtenerDatosFormulario(securitygroup);
			securityDelegate.updatedSecurityGroup(securitygroup);
		}
		limpiarMaestro();
		buscarMaestro(); 
		log.info("fin guardarMaestro");
		}

	@Override
	public void buttonClick(ClickEvent event) {
		if(event.getButton().equals(btnEliminarMaestro)){
			eliminarMaestro();
		}else if(event.getButton().equals(btnGuardarMaestro)){
			guardarMaestro();
		}else if(event.getButton().equals(btnQuitar)){
			eliminarDetalle();
		}else if(event.getButton().equals(btnAadir)){
			guardarDetalle();
		}
	}

	@Override
	public void buscarDetalle() {
		buscarDetalle(null);
	}

	@Override
	public void buscarDetalle(TextChangeEvent event) {
		BeanItemContainer<ParameterDetail> items = new BeanItemContainer<ParameterDetail>(ParameterDetail.class);
		if(securitygroup!=null){
		items.addAll(securityDelegate.findAllSecGroupModuleNoAsig(securitygroup));
		}
		listsModulos_Nuevos.setContainerDataSource(items);
		listsModulos_Nuevos.setItemCaptionPropertyId("name");
		BeanItemContainer<ParameterDetail> items2 = new BeanItemContainer<ParameterDetail>(ParameterDetail.class);
		if(securitygroup!=null){
		items2.addAll(securityDelegate.findAllSecGroupModuleSiAsig(securitygroup));
		}
		ListsModulos.setContainerDataSource(items2);
		ListsModulos.setItemCaptionPropertyId("name");
	}
	

	@Override
	public void limpiarDetalle() {
	
	}

	@Override
	public void guardarDetalle() {
		Set lista = (Set) listsModulos_Nuevos.getValue();
		
		for (Iterator iterator = lista.iterator(); iterator.hasNext();) {
			ParameterDetail module = (ParameterDetail) iterator.next();
			SecGroupModule secGroupModule= new SecGroupModule(new SecGroupModulePK(securitygroup.getIdSecurityGroup(), module.getIdParameterDetail()));
			secGroupModule.setModule(module);
			securityDelegate.createSecGroupModule(secGroupModule);
		}
		buscarDetalle();
	}
	
	@Override
	public void eliminarDetalle() {
		Set lista1 = (Set) ListsModulos.getValue();
		
		for (Iterator iterator = lista1.iterator(); iterator.hasNext();) {
			ParameterDetail module = (ParameterDetail) iterator.next();
			SecGroupModule secGroupModule= new SecGroupModule(new SecGroupModulePK(securitygroup.getIdSecurityGroup(), module.getIdParameterDetail()));
			secGroupModule.setModule(module);
			securityDelegate.deleteSecGroupModule(secGroupModule);
		}
		buscarDetalle();
	}
	
	public void obtenerDatosFormulario(SecurityGroup secugroup){
		
		secugroup.setName(getStringData(txtNombreMaestro));
		secugroup.setDescription(getStringData(txtDescripcion));
		
	}
	
	
	@AutoGenerated
	private VerticalLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new VerticalLayout();
		mainLayout.setImmediate(false);
		mainLayout.setWidth("882px");
		mainLayout.setHeight("800px");
		mainLayout.setMargin(false);
		mainLayout.setSpacing(true);
		
		// top-level component properties
		setWidth("882px");
		setHeight("800px");
		
		// verticalLayout_2
		verticalLayout_2 = buildVerticalLayout_2();
		mainLayout.addComponent(verticalLayout_2);
		
		return mainLayout;
	}

	@AutoGenerated
	private VerticalLayout buildVerticalLayout_2() {
		// common part: create layout
		verticalLayout_2 = new VerticalLayout();
		verticalLayout_2.setImmediate(false);
		verticalLayout_2.setWidth("850px");
		verticalLayout_2.setHeight("500px");
		verticalLayout_2.setMargin(false);
		verticalLayout_2.setSpacing(true);
		
		// lytMaestro
		lytMaestro = buildLytMaestro();
		verticalLayout_2.addComponent(lytMaestro);
		
		// lytDetalle
		lytDetalle = buildLytDetalle();
		verticalLayout_2.addComponent(lytDetalle);
		
		return verticalLayout_2;
	}

	@AutoGenerated
	private HorizontalLayout buildLytMaestro() {
		// common part: create layout
		lytMaestro = new HorizontalLayout();
		lytMaestro.setImmediate(false);
		lytMaestro.setWidth("-1px");
		lytMaestro.setHeight("-1px");
		lytMaestro.setMargin(false);
		
		// lytTablaMaestra
		lytTablaMaestra = buildLytTablaMaestra();
		lytMaestro.addComponent(lytTablaMaestra);
		
		// pnlFormulario
		pnlFormulario = buildPnlFormulario();
		lytMaestro.addComponent(pnlFormulario);
		
		return lytMaestro;
	}

	@AutoGenerated
	private HorizontalLayout buildLytTablaMaestra() {
		// common part: create layout
		lytTablaMaestra = new HorizontalLayout();
		lytTablaMaestra.setImmediate(false);
		lytTablaMaestra.setWidth("570px");
		lytTablaMaestra.setHeight("320px");
		lytTablaMaestra.setMargin(false);
		lytTablaMaestra.setSpacing(true);
		
		// tblfMaestro
		tblfMaestro = new TableCustom();
		tblfMaestro.setImmediate(false);
		tblfMaestro.setWidth("-1px");
		tblfMaestro.setHeight("-1px");
		lytTablaMaestra.addComponent(tblfMaestro);
		
		return lytTablaMaestra;
	}

	@AutoGenerated
	private VerticalLayout buildPnlFormulario() {
		// common part: create layout
		pnlFormulario = new VerticalLayout();
		pnlFormulario.setImmediate(false);
		pnlFormulario.setWidth("-1px");
		pnlFormulario.setHeight("-1px");
		pnlFormulario.setMargin(false);
		pnlFormulario.setSpacing(true);
		
		// label_1
		label_1 = new Label();
		label_1.setImmediate(false);
		label_1.setWidth("-1px");
		label_1.setHeight("-1px");
		label_1.setValue("Formulario Registro");
		pnlFormulario.addComponent(label_1);
		pnlFormulario.setComponentAlignment(label_1, new Alignment(20));
		
		// txtNombreMaestro
		txtNombreMaestro = new TextField();
		txtNombreMaestro.setImmediate(false);
		txtNombreMaestro.setDescription("Nombre");
		txtNombreMaestro.setWidth("200px");
		txtNombreMaestro.setHeight("-1px");
		txtNombreMaestro.setInputPrompt("Nombre");
		pnlFormulario.addComponent(txtNombreMaestro);
		
		// txtDescripcion
		txtDescripcion = new TextField();
		txtDescripcion.setImmediate(false);
		txtDescripcion.setDescription("Descripcion");
		txtDescripcion.setWidth("200px");
		txtDescripcion.setHeight("-1px");
		txtDescripcion.setInputPrompt("Descripcion");
		pnlFormulario.addComponent(txtDescripcion);
		
		// lytBotonera
		lytBotonera = buildLytBotonera();
		pnlFormulario.addComponent(lytBotonera);
		
		return pnlFormulario;
	}

	@AutoGenerated
	private HorizontalLayout buildLytBotonera() {
		// common part: create layout
		lytBotonera = new HorizontalLayout();
		lytBotonera.setImmediate(false);
		lytBotonera.setWidth("-1px");
		lytBotonera.setHeight("-1px");
		lytBotonera.setMargin(false);
		lytBotonera.setSpacing(true);
		
		// btnGuardarMaestro
		btnGuardarMaestro = new Button();
		btnGuardarMaestro.setCaption("Guardar");
		btnGuardarMaestro.setImmediate(true);
		btnGuardarMaestro.setWidth("-1px");
		btnGuardarMaestro.setHeight("-1px");
		lytBotonera.addComponent(btnGuardarMaestro);
		
		// btnEliminarMaestro
		btnEliminarMaestro = new Button();
		btnEliminarMaestro.setCaption("Eliminar");
		btnEliminarMaestro.setImmediate(true);
		btnEliminarMaestro.setWidth("-1px");
		btnEliminarMaestro.setHeight("-1px");
		lytBotonera.addComponent(btnEliminarMaestro);
		
		return lytBotonera;
	}

	@AutoGenerated
	private HorizontalLayout buildLytDetalle() {
		// common part: create layout
		lytDetalle = new HorizontalLayout();
		lytDetalle.setImmediate(false);
		lytDetalle.setWidth("-1px");
		lytDetalle.setHeight("-1px");
		lytDetalle.setMargin(false);
		lytDetalle.setSpacing(true);
		
		// ListsModulos
		ListsModulos = new ListSelect();
		ListsModulos.setImmediate(false);
		ListsModulos.setWidth("150px");
		ListsModulos.setHeight("-1px");
		lytDetalle.addComponent(ListsModulos);
		
		// verticalLayout_4
		verticalLayout_4 = buildVerticalLayout_4();
		lytDetalle.addComponent(verticalLayout_4);
		
		// listsModulos_Nuevos
		listsModulos_Nuevos = new ListSelect();
		listsModulos_Nuevos.setImmediate(false);
		listsModulos_Nuevos.setWidth("150px");
		listsModulos_Nuevos.setHeight("-1px");
		lytDetalle.addComponent(listsModulos_Nuevos);
		
		return lytDetalle;
	}

	@AutoGenerated
	private VerticalLayout buildVerticalLayout_4() {
		// common part: create layout
		verticalLayout_4 = new VerticalLayout();
		verticalLayout_4.setImmediate(false);
		verticalLayout_4.setWidth("-1px");
		verticalLayout_4.setHeight("-1px");
		verticalLayout_4.setMargin(false);
		
		// btnQuitar
		btnQuitar = new Button();
		btnQuitar.setCaption(">");
		btnQuitar.setImmediate(true);
		btnQuitar.setWidth("-1px");
		btnQuitar.setHeight("-1px");
		verticalLayout_4.addComponent(btnQuitar);
		
		// btnAadir
		btnAadir = new Button();
		btnAadir.setCaption("<");
		btnAadir.setImmediate(true);
		btnAadir.setWidth("-1px");
		btnAadir.setHeight("-1px");
		verticalLayout_4.addComponent(btnAadir);
		
		return verticalLayout_4;
	}

}
